package cn.uikit.demo.utils

import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

object CrashLogUtil {
    fun getCrashLog(e: Throwable): String {
        val stringBuilder = StringBuilder()
        stringBuilder.append("Crash Time: ")
        stringBuilder.append(
            SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(
                Date()
            )
        )
        stringBuilder.append("\n")
        stringBuilder.append("Error: ")
        stringBuilder.append(e.toString())
        stringBuilder.append("\n")
        stringBuilder.append("StackTrace: \n")

        val stackTrace = e.stackTrace
        for (element in stackTrace) {
            stringBuilder.append(element.toString())
            stringBuilder.append("\n")
        }
        return stringBuilder.toString()
    }

     fun getLogcatLogs(): String {
        try {
            val process = Runtime.getRuntime().exec("logcat -d -t 500")  // 获取所有的 logcat 日志
            val reader = process.inputStream.bufferedReader()
            return reader.readText()
        } catch (e: IOException) {
            e.printStackTrace()
            return "无法获取 logcat 信息"
        }
    }
}

